package com.init.iblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.entity.Menu;
import com.init.iblog.domain.entity.Role;
import com.init.iblog.domain.vo.RoleVo;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.mapper.RoleMapper;
import com.init.iblog.service.RoleService;
import com.init.iblog.utils.BeanCopyUtils;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-03-04 23:16:07
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {

        //判断是否是管理员，如果是返回集合中只需要admin
        if(id == 3L){     //user表中id为3的包子账号为超级管理员
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息

        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    /**
     * 查询角色列表
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @return
     */
    @Override
    public ResponseResult getRoleList(Integer pageNum, Integer pageSize, String roleName,String status) {

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(roleName)){
            queryWrapper.eq(Role::getRoleName,roleName);
        }
        if(StringUtils.hasText(status)){
            queryWrapper.eq(Role::getStatus,status);
        }
        List<Role> roleList = list(queryWrapper);
        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(roleList, RoleVo.class);
        System.out.println("-----------》"+roleVos.toString());
        return ResponseResult.okResult(roleVos);
    }

    @Override
    public ResponseResult updateRole(Role role) {
        roleMapper.updateById(role);
        return ResponseResult.okResult();
    }


    /**
     * 删除角色
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteRole(List<Long> ids) {

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        for(Long id:ids){
            roleMapper.deleteById(id);
        }
        return ResponseResult.okResult();
    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @Override
    public ResponseResult addRole(Role role) {

        if(!StringUtils.hasText(role.getRoleName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(role);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRole(Long id) {

        Role role = roleMapper.selectById(id);

        return ResponseResult.okResult(role);
    }
}
